package br.com.covosys.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Map;
import java.util.Properties;


public class BasicUtils {
	
	public static String concatIgnoreNull(String... strs) {
		
		StringBuffer sb = new StringBuffer();
		
		concatIgnoreNull(sb, strs);
		
		return sb.toString();
		
	}

	public static void concatIgnoreNull(StringBuffer sb, String... strs){
		
		for (String string : strs) {
			
			if (string != null){
				
				sb.append(string);
				
			}
			
		}
		
	}
	
	public static String concatena(String... strs){
		
		StringBuffer sb = new StringBuffer();
		
		concatena(sb, strs);
		
		return sb.toString();
		
	}
	
	public static void concatena(StringBuffer sb, String... strs){
		
		for (String string : strs) {
			
			sb.append(string);
			
		}
		
	}

	public static String concatena(Object... strs){
		
		StringBuffer sb = new StringBuffer();
		
		concatena(sb, strs);
		
		return sb.toString();
		
	}
	
	public static String concatIgnoreNull(Object... strs){
		
		StringBuffer sb = new StringBuffer();
		
		concatIgnoreNull(sb, strs);
		
		return sb.toString();
		
	}
	
	public static void concatIgnoreNull(StringBuffer sb, Object... objs){
		
		for (Object obj : objs) {
			
			if (obj != null){
				
				sb.append(obj.toString());
			
			}
			
		}
		
	}
	
	public static void concatena(StringBuffer sb, Object... objs){
		
		for (Object obj : objs) {
			
			sb.append(obj.toString());
			
		}
		
	}
	
	public static boolean isEmpty(Object obj){
		
		boolean isEmpty;
		
		if (obj instanceof String){
			
			isEmpty = isEmpty((String) obj); 
			
		}else if (obj instanceof Collection){
			
			isEmpty = isEmpty((Collection<?>) obj); 
			
		}else{
			
			isEmpty = obj == null;
			
		}
		
		return isEmpty;
		
	}
	
	public static boolean isEmpty(Collection<?> col){
		
		return col == null || col.isEmpty();
		
	}
	
	public static boolean isEmpty(Map<?, ?> col){
		
		return col == null || col.isEmpty();
		
	} 
	
	public static boolean isEmpty(String str){
		
		return str == null || str.equals("");
		
	}
	
	public static boolean isEmpty(Object[] array){
		
		return array == null || array.length == 0;
		
	}
	
	public static Properties getProperties(String fileName) throws IOException  {

		Properties properties = null;

		//Try get property in classPath
		InputStream is = BasicUtils.class.getClassLoader().getResourceAsStream(fileName);
		
		//Verify if exist property in classPath, if not, try get file by complete name.
		if (is == null){
			File f = new File(fileName);
			if (f.exists()){
				is = new FileInputStream(f);
			}			
		}
		
		//If din't find the property's file, don't try create properties;  
		if (is != null){
			properties = new Properties();
			properties.load(is);
		}
		
		return properties;
		
	}

	public static Properties getProperties(File file) throws FileNotFoundException, IOException {

		Properties properties;
		FileInputStream fis;
		properties = new Properties();
		fis = new FileInputStream(file);
		properties.load(fis);
		return properties;
	}

	public static void createFile(File f) throws IOException {

		if (!f.getParentFile().exists()){
			
			f.getParentFile().mkdirs();
			
		}
		
		f.createNewFile();
		
		
	}

}
